<?php

namespace app\controller\api\module;

class BaseController extends \app\BaseController
{
    /**
     * 刷新Token的Token
     * @param $userInfo  array 用户ID
     * @return string
     */
    protected function makeRefreshToken(array $userInfo): string
    {
        if(empty($userInfo))return '';
        $refreshToken = md5(json_encode($userInfo) . time() . IP . time() . '000' . 'refreshtoken' . mt_rand(0, 9999));
        return setAPICaches($refreshToken, $userInfo, config('api.refreshTokenTimeOut', 5 * 86400)) ? $refreshToken : '';
    }

    /**
     * 生成用户Token
     * @param $userInfo  array 用户ID
     * @param $refreshToken    string  刷新Token的Token
     * @return string
     */
    protected function makeToken(array $userInfo, string $refreshToken): string
    {
        if(empty($refreshToken))return '';
        $token = md5(json_encode($userInfo) . time() . IP . $refreshToken . '000' . 'token' . mt_rand(0, 9999));
        return setAPICaches($token, $userInfo, config('api.tokenTimeOut', 5 * 60)) ? $token : '';
    }

    protected function getToken(string $token, string $refreshToken): string
    {
        return getAPICaches($token) ? $token : $this->makeToken(getAPICaches($refreshToken, []), $refreshToken);
    }
}